// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch.Snapshot
{
	internal sealed class RepositorySettingsConverter : JsonConverter<RepositorySettings>
	{
		public override RepositorySettings Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
		{
			if (reader.TokenType != JsonTokenType.StartObject)
				throw new JsonException("Unexpected JSON detected.");
			var variant = new RepositorySettings();
			while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
			{
				if (reader.TokenType == JsonTokenType.PropertyName)
				{
					var property = reader.GetString();
					if (property == "chunk_size")
					{
						variant.ChunkSize = JsonSerializer.Deserialize<string?>(ref reader, options);
						continue;
					}

					if (property == "compress")
					{
						variant.Compress = JsonSerializer.Deserialize<Union<string?, bool?>?>(ref reader, options);
						continue;
					}

					if (property == "concurrent_streams")
					{
						variant.ConcurrentStreams = JsonSerializer.Deserialize<Union<string?, int?>?>(ref reader, options);
						continue;
					}

					if (property == "location")
					{
						variant.Location = JsonSerializer.Deserialize<string>(ref reader, options);
						continue;
					}

					if (property == "read_only" || property == "readonly")
					{
						variant.ReadOnly = JsonSerializer.Deserialize<Union<string?, bool?>?>(ref reader, options);
						continue;
					}
				}
			}

			return variant;
		}

		public override void Write(Utf8JsonWriter writer, RepositorySettings value, JsonSerializerOptions options)
		{
			writer.WriteStartObject();
			if (!string.IsNullOrEmpty(value.ChunkSize))
			{
				writer.WritePropertyName("chunk_size");
				writer.WriteStringValue(value.ChunkSize);
			}

			if (value.Compress is not null)
			{
				writer.WritePropertyName("compress");
				JsonSerializer.Serialize(writer, value.Compress, options);
			}

			if (value.ConcurrentStreams is not null)
			{
				writer.WritePropertyName("concurrent_streams");
				JsonSerializer.Serialize(writer, value.ConcurrentStreams, options);
			}

			writer.WritePropertyName("location");
			writer.WriteStringValue(value.Location);
			if (value.ReadOnly is not null)
			{
				writer.WritePropertyName("read_only");
				JsonSerializer.Serialize(writer, value.ReadOnly, options);
			}

			writer.WriteEndObject();
		}
	}

	[JsonConverter(typeof(RepositorySettingsConverter))]
	public sealed partial class RepositorySettings
	{
		public string? ChunkSize { get; set; }

		public Union<string?, bool?>? Compress { get; set; }

		public Union<string?, int?>? ConcurrentStreams { get; set; }

		public string Location { get; set; }

		public Union<string?, bool?>? ReadOnly { get; set; }
	}

	public sealed partial class RepositorySettingsDescriptor : SerializableDescriptorBase<RepositorySettingsDescriptor>
	{
		internal RepositorySettingsDescriptor(Action<RepositorySettingsDescriptor> configure) => configure.Invoke(this);
		public RepositorySettingsDescriptor() : base()
		{
		}

		private string? ChunkSizeValue { get; set; }

		private Union<string?, bool?>? CompressValue { get; set; }

		private Union<string?, int?>? ConcurrentStreamsValue { get; set; }

		private string LocationValue { get; set; }

		private Union<string?, bool?>? ReadOnlyValue { get; set; }

		public RepositorySettingsDescriptor ChunkSize(string? chunkSize)
		{
			ChunkSizeValue = chunkSize;
			return Self;
		}

		public RepositorySettingsDescriptor Compress(Union<string?, bool?>? compress)
		{
			CompressValue = compress;
			return Self;
		}

		public RepositorySettingsDescriptor ConcurrentStreams(Union<string?, int?>? concurrentStreams)
		{
			ConcurrentStreamsValue = concurrentStreams;
			return Self;
		}

		public RepositorySettingsDescriptor Location(string location)
		{
			LocationValue = location;
			return Self;
		}

		public RepositorySettingsDescriptor ReadOnly(Union<string?, bool?>? readOnly)
		{
			ReadOnlyValue = readOnly;
			return Self;
		}

		protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
		{
			writer.WriteStartObject();
			if (!string.IsNullOrEmpty(ChunkSizeValue))
			{
				writer.WritePropertyName("chunk_size");
				writer.WriteStringValue(ChunkSizeValue);
			}

			if (CompressValue is not null)
			{
				writer.WritePropertyName("compress");
				JsonSerializer.Serialize(writer, CompressValue, options);
			}

			if (ConcurrentStreamsValue is not null)
			{
				writer.WritePropertyName("concurrent_streams");
				JsonSerializer.Serialize(writer, ConcurrentStreamsValue, options);
			}

			writer.WritePropertyName("location");
			writer.WriteStringValue(LocationValue);
			if (ReadOnlyValue is not null)
			{
				writer.WritePropertyName("read_only");
				JsonSerializer.Serialize(writer, ReadOnlyValue, options);
			}

			writer.WriteEndObject();
		}
	}
}